package com.yan.crowd.service.Impl;

import com.yan.crowd.entity.po.MemberConfirmInfoPO;
import com.yan.crowd.entity.po.MemberLaunchInfoPO;
import com.yan.crowd.entity.po.ProjectPO;
import com.yan.crowd.entity.po.ReturnPO;
import com.yan.crowd.entity.vo.*;
import com.yan.crowd.mapper.*;
import com.yan.crowd.service.ProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.naming.ldap.PagedResultsControl;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Transactional(readOnly = true)
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectPOMapper projectPOMapper;

    @Autowired
    private ProjectItemPicPOMapper projectItemPicPOMapper;

    @Autowired
    private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;

    @Autowired
    private MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;

    @Autowired
    private ReturnPOMapper returnPOMapper;

    @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveProjectVORemote(ProjectVo projectVo, Integer memberId) {

        ProjectPO projectPO = new ProjectPO();
        BeanUtils.copyProperties(projectVo, projectPO);

        projectPOMapper.insertSelective(projectPO);
        Integer projectId = projectPO.getId();

        // 保存项目、分类关联信息
        List<Integer> typeIdList = projectVo.getTypeIdList();
        projectPOMapper.insertRelationship(typeIdList, projectId);

        // 保存项目、标签关联信息
        List<Integer> tagIdList = projectVo.getTagIdList();
        projectPOMapper.insertTagRelationship(tagIdList, projectId);

        // 保存项目详情图片信息
        List<String> detailPicturePathList = projectVo.getDetailPicturePathList();
        projectItemPicPOMapper.insertPathList(projectId, detailPicturePathList);

        // 保存项目发起人信息
        MemberLauchInfoVO memberLauchInfoVO = projectVo.getMemberLauchInfoVO();
        MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
        BeanUtils.copyProperties(memberLauchInfoVO, memberLaunchInfoPO);
        memberLaunchInfoPO.setMemberid(memberId);
        memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);

        // 保存项目回报信息
        List<ReturnVO> returnVOList = projectVo.getReturnVOList();
        List<ReturnPO> returnPoList = new ArrayList<>();
        for (ReturnVO returnVO : returnVOList) {
            ReturnPO returnPO = new ReturnPO();
            BeanUtils.copyProperties(returnVO,returnPO);
            returnPoList.add(returnPO);
        }

        returnPOMapper.insertReturnPoBatch(returnPoList, memberId);


        // 保存项目确认信息
        MemberConfirmInfoVO memberConfirmInfoVO = projectVo.getMemberConfirmInfoVO();
        MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO();
        BeanUtils.copyProperties(memberConfirmInfoVO, memberConfirmInfoPO);
        memberConfirmInfoPO.setMemberid(memberId);
        memberConfirmInfoPOMapper.insert(memberConfirmInfoPO);
    }

    @Override
    public List<PortalTypeVO> getPortalTypeVO() {
        return projectPOMapper.selectPortalTypeVOList();
    }

    @Override
    public DetailProjectVO getDetailProjectVo(Integer id) {

        DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(id);

        Integer status = detailProjectVO.getStatus();

        switch (status){
            case 0:
                detailProjectVO.setStatusText("审核中");
                break;
            case 1:
                detailProjectVO.setStatusText("众筹中");
                break;
            case 2:
                detailProjectVO.setStatusText("众筹成功");
                break;
            case 3:
                detailProjectVO.setStatusText("已关闭");
                break;

        }
        Integer deployDate = detailProjectVO.getDeployDate();
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = dateFormat.parse(String.valueOf(deployDate));
            long nowTime = date.getTime();
            long deployTime = parse.getTime();
            long needTime = (nowTime - deployTime) / 1000 / 60 / 60 / 24;

            Integer day = detailProjectVO.getDay();
            Integer lastDay = Math.toIntExact(day - needTime);
            detailProjectVO.setLastDay(lastDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }
}
